/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       view_group.cpp
 * Author:     k.swiniarsk2
 * Created on: Sep 2, 2013
 *
 **********************************************************************************************************************/

#include "view_group.h"

namespace mGameEngine
{
namespace GUI
{

ViewGroup::ViewGroup() :
		View(),
		_views()
{
}

ViewGroup::~ViewGroup()
{
	for(List<View *>::Iterator i = _views.begin(); i != _views.end(); ++i)
	{
		delete *i;
	}
}

void ViewGroup::addView(View* view, uint8 position)
{
	// no null views
	if(!view)
	{
		return;
	}

	// detach from previous parent
	if(view->_parent)
	{
		view->_parent->removeView(view);
	}

	// add to list
	if(position == -1)
	{
		_views.push_back(view);
	}
	else
	{
		List<View *>::Iterator i = _views.begin();
		while(position > 0 && i != _views.end())
		{
			--position;
			++i;
		}
		_views.insert(i, view);
	}

	// set parent view
	view->_parent = this;
}

void ViewGroup::removeView(View* view)
{
	// no null views
	if(!view)
	{
		return;
	}

	// remove view
	_views.remove(view);
	view->parent = NULL;
}

void ViewGroup::removeAllViews()
{
	for(List<View *>::Iterator i = _views.begin(); i != _views.end(); ++i)
	{
		View *view = *i;
		view->_parent = NULL;
	}
	_views.clear();
}

} // namespace GUI
} // namespace mGameEngine
